home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume16 / dosdu / part01 next >
Encoding:
Internet Message Format  |  1991-01-02  |  9.6 KB

  1. From: kent@sparky.IMD.Sterling.COM (Kent Landfield)
  2. Newsgroups: comp.sources.misc
  3. Subject: v16i001:  dosdu - DOS version of the du command, Part01/01
  4. Message-ID: <1991Jan3.064348.4713@sparky.IMD.Sterling.COM>
  5. Date: 3 Jan 91 06:43:48 GMT
  6. Approved: kent@sparky.imd.sterling.com
  7. X-Checksum-Snefru: 778d6b8a a4b86ad0 86f88c2f 576bb0ae
  8.  
  9. Submitted-by: uunet!stimage.mqcc.mq.oz.au!mallsop (Mark Allsop)
  10. Posting-number: Volume 16, Issue 1
  11. Archive-name: dosdu/part01
  12.  
  13. Hi,
  14.   Here's a unix-like du program for MSDOS.  I hope people find it helpful.
  15. Cheers,
  16. -Mark.
  17.  
  18.  Mark Allsop                                              Computer Scientist 
  19.  email: mallsop@suna.mqcc.mq.oz.au                The Statistical Laboratory 
  20.  Phone: At MacUni: (61 2) 805-8592  / \      Macquarie University, Australia 
  21.  Fax  :          : (61 2) 805-7433   |   This one goes up to 11.....
  22.  
  23. #! /bin/sh
  24. # This is a shell archive.  Remove anything before this line, then feed it
  25. # into a shell via "sh file" or similar.  To overwrite existing files,
  26. # type "sh file -c".
  27. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  28. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  29. # If this archive is complete, you will see the following message at the end:
  30. #        "End of shell archive."
  31. # Contents:  du.c
  32. # Wrapped by kent@sparky on Thu Jan  3 00:42:17 1991
  33. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  34. if test -f 'du.c' -a "${1}" != "-c" ; then 
  35.   echo shar: Will not clobber existing file \"'du.c'\"
  36. else
  37. echo shar: Extracting \"'du.c'\" \(7543 characters\)
  38. sed "s/^X//" >'du.c' <<'END_OF_FILE'
  39. X Fax  :          : (61 2) 805-7433   |   This one goes up to 11.....
  40. X
  41. X/*
  42. X *    du.c: Calculate disk usage similar to UNIX version
  43. X *
  44. X * Compiles under TC.  May need mods for MSC.
  45. X *
  46. X * PLEASE : If you make any improvements, send a copy to me so I can make
  47. X *               changes available to everyone.....
  48. X *
  49. X *  Author: Mark Allsop (Internet address: mallsop@suna.mqcc.mq.oz.au)
  50. X *
  51. X *  See #define VERSION and __DATE__ in compile for current version and date
  52. X *
  53. X *  This software is placed into the public domain.  The author grants the right
  54. X * to anyone to alter this program, provided that any upgrades or modifications
  55. X * are mailed back to him.
  56. X *  This program may not be sold for any profit.  The source code may, however ,
  57. X * be used as part of any other package PROVIDED appropriate credit is given in
  58. X * the program source code AND documentation and that no fee is ever charged
  59. X * for just the code in this program.  This entire comment block MUST appear
  60. X * in the code at the place of usage of the code in this module.
  61. X *  For further information, contact the author!
  62. X *  This disclaimer is intended to stop people ripping off my software, and
  63. X * using what was free and making money from it.  I call such people *scum*.
  64. X * I encourage you to release other utilities into the public domain so that
  65. X * life can be made easier for all of us.
  66. X */
  67. X
  68. X/*
  69. X * This currently changes the directories to find files.  This is slow, but I
  70. X * havn't had the time to do it properly yet.  You should just use strings
  71. X * and the path and cat/uncat them.
  72. X */
  73. X
  74. X/*
  75. X * Options:-
  76. X *    -a: generate an entry for every file
  77. X *    -s: only display grand total for each of the file names
  78. X *    -r: display error info about unreadable directories, etc.
  79. X *      -k: display sizes in Kb
  80. X *      -m: display sizes in Mb
  81. X */
  82. X
  83. X#include <conio.h>
  84. X#include <stdio.h>
  85. X#include <string.h>
  86. X#include <dos.h>
  87. X#include <dir.h>
  88. X
  89. X#define VERSION    "1.0"
  90. X#define MAXPATHLEN 100
  91. X#define conv_size(size)    ((size_type == KB) ? (size) / 1024 : (size_type==MB) \
  92. X                          ? (size) / 1048576 : (size))
  93. X
  94. Xint c_break(void);
  95. Xvoid sw_flags (int *ac, char **av, char *letters, int *flags);
  96. Xunsigned long find_usage(char *path, int quiet);
  97. Xvoid du_help(char *fname);
  98. X
  99. Xenum Size_type {BYTES, KB, MB} size_type;
  100. X
  101. Xint all, disp_errs;
  102. Xint curr_disk, diff_drv;
  103. Xchar cwd_org[MAXPATHLEN], cwd_org2[MAXPATHLEN];
  104. X
  105. Xmain(int ac, char **av)
  106. X{
  107. X    /* all & disp_errs are global to save stack space during recusrion */
  108. X  int i, j, flags[5], quiet;
  109. X  char dir[MAXPATHLEN];
  110. X
  111. X  if (av[1][0] == '?') {
  112. X    du_help(av[0]);
  113. X    return(0);
  114. X  }
  115. X
  116. X  ctrlbrk(c_break);    /* Catch ctrl-c's so you don't get left elsewhere */
  117. X
  118. X  sw_flags(&ac, av, "arskm", flags);
  119. X  all = flags[0];
  120. X  disp_errs = flags[1];
  121. X  quiet = flags[2];
  122. X  size_type = (flags[4]) ? MB : ((flags[3]) ? KB : BYTES);
  123. X
  124. X  printf("Sizes given are in %s\n", (size_type == KB) ? "Kb"
  125. X                 : (size_type == MB) ? "Mb" : "bytes");
  126. X  getcwd(cwd_org, MAXPATHLEN);    /* Save cwd so we can return to it */
  127. X  curr_disk = getdisk();
  128. X  if (ac == 1) {
  129. X    strcpy(dir, ".");
  130. X    printf("%12ld %s\n", conv_size(find_usage(dir, quiet)), dir);
  131. X  } else {
  132. X    for (i=1; i<ac; i++) {
  133. X    strcpy(dir, av[i]);
  134. X    if (dir[1] == ':' && strlen(dir) >= 2) {
  135. X      setdisk(toupper(dir[0])-'A');
  136. X      getcwd(cwd_org2, MAXPATHLEN);    /* Save cwd so we can return to it */
  137. X      diff_drv=1;    /* Diff drive - need to restore 2wice *
  138. X
  139. X      j=2;
  140. X      while(dir[j] != '\0'){
  141. X    dir[j-2] = dir[j];
  142. X    j++;
  143. X      }
  144. X      dir[j-2] = dir[j];
  145. X      if (dir[0] == '\0')    /* No name! Can't have that now, can we! */
  146. X        strcpy(dir, ".");
  147. X    }
  148. X    else
  149. X      diff_drv=0;        /* Same drive as before */
  150. X    if (chdir(dir) == 0)    /* Directory exists */
  151. X      printf("%12ld %s\n", conv_size(find_usage(dir, quiet)), dir);
  152. X    else
  153. X      printf("%s: No such file or directory\n", dir);
  154. X    setdisk(curr_disk);
  155. X    chdir(cwd_org);        /* Return to original directory */
  156. X    }
  157. X    if (diff_drv) {
  158. X      chdir(cwd_org2);        /* Reset for each command */
  159. X      diff_drv=0;
  160. X    }
  161. X  }
  162. X  if (diff_drv)
  163. X    chdir(cwd_org2);        /* Reset for each command */
  164. X  setdisk(curr_disk);
  165. X  chdir(cwd_org);        /* Return to original directory */
  166. X  return(0);
  167. X}
  168. X
  169. Xunsigned long find_usage(char *path, int quiet)
  170. X{
  171. X  char dpath[MAXPATHLEN];
  172. X  struct ffblk fblk;
  173. X  unsigned long size = 0, tsiz;
  174. X
  175. X  if (findfirst("*.*", &fblk, 0xff) == 0) {
  176. X    do {
  177. X      if (!strcmp(fblk.ff_name, ".") || !strcmp(fblk.ff_name, ".."))
  178. X    continue;
  179. X      switch (fblk.ff_attrib) {
  180. X    case FA_DIREC:
  181. X      size += (unsigned long)fblk.ff_fsize;
  182. X      if (chdir(fblk.ff_name) == 0) {
  183. X        sprintf(dpath, "%s\\%s", path, fblk.ff_name);
  184. X        tsiz = find_usage(dpath, quiet);
  185. X        if (!quiet)
  186. X          printf("%12ld %s\n", conv_size(tsiz), dpath);
  187. X        size += tsiz;
  188. X        chdir("..");
  189. X          }
  190. X          else if (disp_errs)
  191. X            printf("%s\\%s: Permission Denied\n", path, fblk.ff_name);
  192. X      break;
  193. X    case FA_LABEL:
  194. X    case FA_RDONLY:
  195. X    case FA_HIDDEN:
  196. X    case FA_SYSTEM:
  197. X    case FA_ARCH:
  198. X    default:
  199. X      size += (unsigned long)fblk.ff_fsize;
  200. X          if (all)
  201. X        printf("%12ld %s\\%s\n", conv_size(fblk.ff_fsize), path,
  202. X                                fblk.ff_name);
  203. X      break;
  204. X      }
  205. X    } while (findnext(&fblk) == 0);
  206. X  }
  207. X  return(size);            /* Default: can't have had any size! */
  208. X}
  209. X
  210. Xvoid du_help(char *fname)
  211. X{
  212. X  printf("%s [-s][-a][-r][-k][-m] [list of names]: disk usage\n", fname);
  213. X  printf("     -s: Only display grand total for each specified name\n");
  214. X  printf("     -a: Generate an entry for each file\n");
  215. X  printf("     -r: Display errors for directories that can't be opened\n");
  216. X  printf("     -k: Display sizes in Kb\n");
  217. X  printf("     -m: Display sizes in Mb\n");
  218. X  printf("list of names: list of file/directory names for usage of.\n");
  219. X  printf(
  220. X"    Author: Mark Allsop, Internet address: mallsop@suna.mqcc.mq.oz.au.\n");
  221. X  printf("    Date: %s.  Version: %s.  Public domain program.\n",
  222. X                              __DATE__, VERSION);
  223. X}
  224. X
  225. Xint c_break(void)
  226. X{
  227. X  printf("User interrupt!\n");
  228. X  if (diff_drv)
  229. X    chdir(cwd_org2);        /* Reset for each command */
  230. X  chdir(cwd_org);        /* Return to original directory */
  231. X  setdisk(curr_disk);
  232. X  exit(1);            /* They want out! */
  233. X}
  234. X
  235. X
  236. X/*
  237. X * swflag.c: Parse flag type switches.
  238. X *
  239. X * sw_flag (&ac, av, "letters", flags)
  240. X *
  241. X * For each letter, if it is found in a switch which contains
  242. X * only valid letters from the sequence "letters" then the corresponding
  243. X * flag is set true, else set false. The flags count the number of occurrences
  244. X
  245. X * of the switch letter. Thus, the program can use repetition for
  246. X * emphasis.
  247. X * e.g. letter "v" for verbosity; -vv would mean extra verbose.
  248. X *
  249. X * HISTORY
  250. X * 19-Sep-86  Leonard Hamey (lgh) at Carnegie-Mellon University
  251. X *    Fixed bug which allowed a lone hyphen to be matched as a flags
  252. X *    argument with no flags.
  253. X *
  254. X *  9-Aug-86  Leonard Hamey (lgh) at Carnegie-Mellon University
  255. X *    Created.
  256. X */
  257. X
  258. Xvoid sw_flags(ac, av, letters, flags)
  259. Xint *ac;
  260. Xregister char **av;
  261. Xregister char *letters;
  262. Xregister int *flags;
  263. X{
  264. X  register char *p;
  265. X  register int i, j;
  266. X  if (letters[0] == '-')
  267. X    letters++;
  268. X  for (i = 0; letters[i] != '\0'; i++)
  269. X    flags[i] = 0;
  270. X  for (i = *ac; --i >= 1; )
  271. X  {
  272. X    p = av[i];
  273. X    if (*p == '-' && p[1] != '\0')
  274. X    {
  275. X      for (p++; *p != '\0'; p++)
  276. X    if (strchr (letters, *p) == 0)
  277. X      break;
  278. X      if (*p != '\0')
  279. X    continue;
  280. X      for (p = av[i]+1; *p != '\0'; p++)
  281. X      { /*
  282. X     * Count occurrences of the flag
  283. X     */
  284. X    flags[strchr(letters,*p) - letters]++;
  285. X      }
  286. X      /*
  287. X       * Discard argument.
  288. X       */
  289. X      (*ac)--;
  290. X      for (j = i; j < *ac; j++)
  291. X    av[j] = av[j+1];
  292. X      av[j] = NULL;
  293. X    }
  294. X  }
  295. X}
  296. X
  297. END_OF_FILE
  298. if test 7543 -ne `wc -c <'du.c'`; then
  299.     echo shar: \"'du.c'\" unpacked with wrong size!
  300. fi
  301. # end of 'du.c'
  302. fi
  303. echo shar: End of shell archive.
  304. exit 0
  305. exit 0 # Just in case...
  306. -- 
  307. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  308. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  309. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  310. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  311.